<?
//echo 'test foo';
class Clscandir
{
    public $path;
	public $sorting_order;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	//把文件转化为文件所在目录字串
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sorting_order=0){
		$path = Clscandir::fileToDirname($path);
		$this->scandir($path , $sorting_order);
		$this->get_complete_filesDirs();
		$this->get_complete_dirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path,$sorting_order=0){
		$this->path = $path ;
		$this->sorting_order = $sorting_order;
		$this->filesDirs=scandir ( $path , $sorting_order ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	public function get_complete_filesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = $this->_format_name($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	//得到完整的目录名和文件名  
	public function get_complete_dirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){
				
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function get_complete_files(){
		return $this->completeFiles;
	}
	private function iva(){}
	protected function ote(){}
	
	public function _scandir($path){
	    $dir = dir($path);
	    $retarr = array( ) ;
        while (($file = $dir->read()) !== false)
        {  $retarr[] =  ($file) ;  }	
	    $dir->close();
	    $retarr["cwd"]=$this->_format_name(getcwd());
	    return $retarr;
    }
	
	public function _format_name($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }
	
	public function show(){
		echo "目录：<br>";
	    secho($this->completeDirs);	
		echo "文件：<br>";
		secho($this->completeFiles);	
	}
	public function demo(){
	    echo str_replace(array("\n"," "),array("<br>","&nbsp;"),
	htmlspecialchars('     $dir=_default_em($_REQUEST["dir"],\"I:/AppServ/www/_doll/");
     $scandir=new CLsandir($dir,0);
     secho($scandir->get_complete_dirs());
     secho($scandir->get_complete_files());') )	;
		
	}
	public function each($arr,$callback=""){
		if(!is_array($arr))return 'Is not array!';
		
		$retarr=array();
		foreach($arr as $k=>$v){
			if(""!=$callback)
			{$retarr[$k]=$callback($v);	}
			else
			{$retarr[$k]=$v;}		
		}
		return $retarr;
	}
	public static function toHttpUrl($arr,
	$middle_path="/_doll/_ae/"){
		$severpath="http://".$_SERVER['SERVER_NAME']
		.":".$_SERVER['SERVER_PORT']
		.$middle_path;
        $retarr=array();
		foreach($arr as $k=>$v){
			 $v=preg_replace(
    //  "/[\d\D]*\/([a-zA-Z0-9_\.\-]*\.(?:flv|f4v|mp[4]|mkv))$/"
	  "/[\d\D]*\/([a-zA-Z0-9_\.\-\[\]\(\)]*\.(?:flv|f4v|mp[4]|mkv))$/"
       ,$severpath."\\1",$v);
	       if(!preg_match("/^[a-zA-Z]\:/",$v))
		   {
			   $retarr[$k]=Clscandir::replacePoundsign($v); 	
		   }	
		}
		return $retarr;		
	}
	public static function replacePoundsign($str){
        $str=iconv('GB2312','UTF-8', $str)  ;
	    $str= preg_replace("/#/","%23",$str);
		return $str;
	}
	
		
	
}


?>